package security.dao;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import security.model.Account;
import security.model.Organization;
import security.model.Role;


@Stateless
public class AccountDAO {

	
	@PersistenceContext(unitName = "persistence")
    private EntityManager em;
	
	public Account getById(Integer id){	
		return em.find(Account.class, id);
	}
	
	public Account save(Account account) {
    	em.persist(account);
    	return account;
    }
          
    public Account update(Account account) {
    	return em.merge(account);
    }
    
    public void delete(Integer id) {
    	em.remove(em.find(Account.class, id));
    }
	
	@SuppressWarnings("unchecked")
    public List<Account> list(int first, int max) {    	
    	Query query = em.createQuery("SELECT e FROM Account e ORDER BY e.id DESC");
    	if (max > 0) {
            query.setMaxResults(max);
        }
        if (first > 0) {
            query.setFirstResult(first);
        }
        return query.getResultList();   
    }

    //----------------------------------------------------------------------
       
    public Account getByUsername(String username){	
		Account result = null;
		Query query = em.createQuery("SELECT e FROM Account e WHERE e.username=:username");
		query.setParameter("username", username);
		try {
			result = (Account) query.getSingleResult(); 
        }
    	catch (NoResultException e) {
			//e.printStackTrace();
		}
        return result;
	}
    
	public Account getByEmail(String email){	
		Account result = null;
		Query query = em.createQuery("SELECT e FROM Account e WHERE e.email=:email");
		query.setParameter("email", email);
		try {
			result = (Account) query.getSingleResult(); 
        }
    	catch (NoResultException e) {
			//e.printStackTrace();
		}
        return result;
	}
   
	public Account getByUsernamePassword(String username, String password){
		Account result = null;
		Query query = em.createQuery("SELECT e FROM Account e WHERE e.username=:username and e.password=:password");
		query.setParameter("username", username);
		query.setParameter("password", password);
		try {
			result = (Account) query.getSingleResult(); 
        }
    	catch (NoResultException e) {
			//e.printStackTrace();
		}		
        return result; 
	}
	
	public Account getByEmailPassword(String email, String password){
		Account result = null;
		Query query = em.createQuery("SELECT e FROM Account e WHERE e.email=:email and e.password=:password");
		query.setParameter("email", email);
		query.setParameter("password", password);
		try {
			result = (Account) query.getSingleResult(); 
        }
    	catch (NoResultException e) {
			//e.printStackTrace();
		}		
        return result; 
	}
	
    public Account getByOrganizationRole(Organization organization, Role role) {
    	Query query = em.createQuery("SELECT e FROM Account e WHERE e.organization=:organization and e.role=:role and e.enabled=:enabled ORDER by e.creation DESC");
        query.setParameter("organization", organization);
        query.setParameter("role", role);
        query.setParameter("enabled", true);
        return (Account) query.getSingleResult();   
    }
	
   
    @SuppressWarnings("unchecked")
    public List<Account> listByOrganization(Organization organization, int first, int max) {
    	Query query = em.createQuery("SELECT e FROM Account e WHERE e.organization=:organization");
    	if (max > 0) {
            query.setMaxResults(max);
        }
        if (first > 0) {
            query.setFirstResult(first);
        }
        query.setParameter("organization", organization);
        return query.getResultList();   
    }
    
    @SuppressWarnings("unchecked")
    public List<Account> listByOrganizationRole(Organization organization, Role role, Integer first, Integer max, Boolean status) {
    	Query query = em.createQuery("SELECT e FROM Account e WHERE e.organization=:organization AND e.role=:role" );
    	query.setParameter("organization", organization);
        query.setParameter("role", role);
    	if (max > 0) {
            query.setMaxResults(max);
        }
        if (first > 0) {
            query.setFirstResult(first);
        }
        return query.getResultList();   
    }    

 
    
}

